//
//  HZL_FourthCollectionViewCell.m
//  B_Project
//
//  Created by 刘峻豪 on 15/11/25.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_FourthCollectionViewCell.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import <AVFoundation/AVFoundation.h>
#import "HZL_PlayerViewController.h"

@interface HZL_FourthCollectionViewCell ()
@property (nonatomic, strong) UIImageView *videoView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *smallImageView;
@property (nonatomic, strong) UIView *smallView;
@property (nonatomic, strong) UIImageView *bg;
@property (nonatomic, strong) UILabel *line;
@property (nonatomic, strong) UILabel *time;
@property (nonatomic, strong) UIButton *begin;
@property (nonatomic, copy) NSString *playUrl;
@property (nonatomic, copy) NSString *title;

@end
@implementation HZL_FourthCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 图片
        self.videoView = [[UIImageView alloc]init];
        self.videoView.contentMode = UIViewContentModeScaleAspectFill;
        self.videoView.clipsToBounds = YES;
        [self.contentView addSubview:self.videoView];
        
        // 背景图
        self.bg = [[UIImageView alloc]init];
        [self.contentView addSubview:self.bg];
        // 背景高斯模糊
        UIBlurEffect *b = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        UIVisualEffectView *bV = [[UIVisualEffectView alloc] initWithEffect:b];
        bV.frame = self.contentView.frame;
        [self.bg addSubview:bV];
        
        //
        self.smallView = [[UIView alloc]init];
        self.smallView.backgroundColor = [UIColor blackColor];
        self.smallView.alpha = 0.6;
        [self.contentView addSubview:self.smallView];
        
        // 标题
        self.titleLabel = [[UILabel alloc]init];
        self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        self.titleLabel.textColor = [UIColor whiteColor];
        [self.contentView addSubview:self.titleLabel];
        
        // 线
        self.line = [[UILabel alloc]init];
        self.line.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:self.line];
        
        // 类型 时间
        self.time = [[UILabel alloc]init];
        self.time.font = [UIFont systemFontOfSize:14];
        self.time.textColor = [UIColor whiteColor];
        [self.contentView addSubview:self.time];
        
        self.desLabel = [[UILabel alloc]init];
        self.desLabel.textColor = [UIColor whiteColor];
        self.desLabel.numberOfLines = 0;
        self.desLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:self.desLabel];
        
        self.begin = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.begin setImage:[UIImage imageNamed:@"iconfont-bofang"] forState:UIControlStateNormal];
        [self.begin addTarget:self action:@selector(playVideo) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.begin];
        

    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    // 图片
    [self.videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width, self.contentView.frame.size.width));
        make.top.mas_equalTo(self.contentView.mas_top).offset(0);
        make.left.mas_equalTo(self.contentView.mas_left).offset(0);
    }];
    
    // 背景图
    [self.bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(WIDTH, [UIScreen mainScreen].bounds.size.height - WIDTH));
        make.top.mas_equalTo(self.videoView.mas_bottom).offset(0);
        make.left.mas_equalTo(self.contentView.mas_left).offset(0);
    }];
    
    [self.smallView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(WIDTH, [UIScreen mainScreen].bounds.size.height - WIDTH));
        make.top.mas_equalTo(self.videoView.mas_bottom).offset(0);
        make.left.mas_equalTo(self.contentView.mas_left).offset(0);
    }];
    
    // 标题
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(WIDTH - 20, 50));
        make.top.mas_equalTo(self.videoView.mas_bottom).offset(0);
        make.left.mas_equalTo(self.contentView.mas_left).offset(10);
    }];
    
    // 线
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(WIDTH - 50, 1));
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(0);
        make.left.mas_equalTo(self.contentView.mas_left).offset(10);
    }];
    
    // 类型 时间
    [self.time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(200, 25));
        make.top.mas_equalTo(self.line.mas_bottom).offset(5);
        make.left.mas_equalTo(self.contentView.mas_left).offset(10);
    }];
    
    // deslabel
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(WIDTH-20, 200));
        make.top.mas_equalTo(self.line.mas_bottom).offset(-20);
        make.left.mas_equalTo(self.contentView.mas_left).offset(10);
    }];
    [self.begin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 100));
        make.center.mas_equalTo(self.videoView);
    }];

}

//赋值
- (void)setCollectionModel:(HZL_FourthViewModel *)collectionModel
{
    // 图片
    [self.videoView sd_setImageWithURL:[NSURL URLWithString: collectionModel.coverForFeed]];
    // 背景图
    [self.bg sd_setImageWithURL:[NSURL URLWithString: collectionModel.coverForFeed]];
    // 标题
    self.titleLabel.text = collectionModel.title;
    
    NSNumber *minute = [NSNumber numberWithInt:[collectionModel.duration intValue] / 60];
    NSNumber *second = [NSNumber numberWithInt:[collectionModel.duration intValue] % 60];
    NSString *time = [NSString stringWithFormat:@" %@' %@'' ", minute, second];
    self.time.text = [NSString stringWithFormat:@"#%@ / %@ ", collectionModel.category, time];
    self.desLabel.text = collectionModel.des;
    
    self.playUrl = collectionModel.playUrl;
    self.title = collectionModel.title;
    
    // 清扫手势
    self.swip = [[UISwipeGestureRecognizer alloc]init];
    self.swip.direction = UISwipeGestureRecognizerDirectionUp;
    [self.smallView addGestureRecognizer:self.swip]; 

}
- (void)playVideo
{
    HZL_PlayerViewController *pVC = [[HZL_PlayerViewController alloc] init];
    pVC.playUrl = self.playUrl;
    pVC.titlee = self.title;
    UIResponder *responder = self.nextResponder.nextResponder.nextResponder;
    [((UIViewController *)responder) presentViewController:pVC animated:YES completion:nil];
}

@end
